package cn.bone.base.boot.demo1.web;

import cn.bone.base.boot.demo1.bean.User;
import cn.bone.base.boot.demo1.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by MD-01 on 2016/9/2.
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    private Logger log = LoggerFactory.getLogger(UserController.class);

    @RequestMapping("/getUser")
    public User getUser(){

        User user = userService.getUser();
        log.info("获取用户:user={}",user);
        return user;
    }

    @RequestMapping("/login")
    public Map<String,String>  login(String username,String password){

        Map<String,String> map = new HashMap<>();
        map.put("userId","1");
        map.put("username","daize");
        map.put("age","18");
        return map;
    }
    @RequestMapping("/img")
    public Map<String,String> img(MultipartFile[] img){
        boolean b = true;
       String msg = null;
        if(img!=null&&img.length>0){
            for(MultipartFile file : img){
                String name = "d:/"+file.getOriginalFilename();
                File saveFile = new File(name);
                try {
                    file.transferTo(saveFile);
                } catch (IOException e) {
                    System.out.println("文件上传出错");
                    msg = "文件上传出错";
                    b = false;
                    e.printStackTrace();
                }
            }
        }else{
            System.out.println("没有上传文件");
            b = false;
            msg = "没有上传文件";
        }
        Map<String,String> result = new HashMap<String,String>();
        if(b){
            result.put("code","0");
            result.put("msg",msg);
        }else{
            result.put("code","1");
            result.put("msg",msg);
        }
        return result;
    }
}
